<template>
	<el-form :model="fruit" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
		<el-form-item label="水果id" prop="id">
			<el-input v-model="fruit.id" readonly></el-input>
		</el-form-item>
		<el-form-item label="水果名称" prop="name">
			<el-input v-model="fruit.name"></el-input>
		</el-form-item>
		<el-form-item label="销量" prop="sale">
			<el-input v-model="fruit.sale"></el-input>
		</el-form-item>
		<el-form-item label="图标" prop="icon">
			<el-input v-model="fruit.icon"></el-input>
		</el-form-item>
		
		<el-form-item>
			<el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
			<el-button @click="resetForm('ruleForm')">重置</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
	import axios from "axios";
	
	export default {
		name: "Edit",
		data() {
			return {
				fruit: {
					id: '',
					name: '',
					sale: '',
					icon: ''
				},
				rules: {
					name: [
						{ required: true, message: '请输入水果名称', trigger: 'blur' },
					],
					sale: [
						{ required: true, message: '请输销量', trigger: 'blur' },
					],
					icon: [
						{ required: true, message: '请输入图标地址', trigger: 'blur' },
					],
					
				}
			};
		},
		
		created() {
			let id = this.$route.query.id
			axios.get('http://localhost:8181/fruit/find/'+id).then(res => {
				this.fruit = res.data
			})
		},
		
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						axios.put('http://localhost:8181/fruit/update',this.fruit).then(res => {
							if (res.data){
								this.$alert(this.fruit.name+'修改成功！','修改数据', {
									confirmButtonText: '确定',
									callback: action => {
										this.$router.push('/table')
									}
								})
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style scoped>

</style>
